package com.qf.service.impl;

import com.qf.constant.CacheConstants;
import com.qf.constant.StrategyConstants;
import com.qf.feign.CacheService;
import com.qf.model.StandardSubmit;
import com.qf.service.StrategyService;
import com.qf.util.PushMsgUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 黑名单策略
 * @author lenovo
 * @version V1.0
 * @Date 2021/11/22 21:37
 */
@Component(value = "black")
@Slf4j
public class BlackFilterServiceImpl implements StrategyService {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private PushMsgUtil pushMsgUtil;

    @Override
    public Boolean execute(StandardSubmit standardSubmit) {
        log.info("【策略模块 - 黑名单策略】开始执行，短信对象内容 standardSubmit={}",standardSubmit);
        //1.获取目标手机号
        String mobile = standardSubmit.getDestMobile();

        //2.根据手机号去redis中查询黑名单数据
        String key = CacheConstants.CACHE_PREFIX_BLACK + mobile;
        String res = cacheService.get(key);

        //3.如果能查到，说明该手机号是黑名单的数据
        if (!StringUtils.isEmpty(res)){
            log.info("【策略模块 - 黑名单策略】，发送失败，目标手机号被拉黑！ mobile={}",mobile);
            pushMsgUtil.pushReport(standardSubmit, StrategyConstants.STRATEGY_ERROR_BLACK);
            pushMsgUtil.sendLog(standardSubmit, StrategyConstants.STRATEGY_ERROR_BLACK);
            return false;
        }
        log.info("【策略模块 - 黑名单策略】实现完毕，短信对象内容 standardSubmit={}",standardSubmit);
        return true;
    }
}
